<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--用户数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
          <el-form-item label="用户名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="请输入公司名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号码" prop="phonenumber">
            <el-input
              v-model="queryParams.phonenumber"
              placeholder="请输入手机号码"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="dateRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
            >修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
            >删除
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="名称" align="center" key="name" prop="name"/>
          <el-table-column label="logo" align="center" key="logo" prop="logo"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <el-image
                v-if="previewListResource"
                style="width: 50px; height: 50px;border-radius: 50%"
                :src="scope.row.logo"
                :preview-src-list="[scope.row.logo]"/>
            </template>
          </el-table-column>
          <el-table-column label="公债明细是否显示" align="center" key="publicOpen">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.publicOpen"
                active-value="0"
                inactive-value="1"
                @change="handlePublicOpenChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="状态" align="center" key="status">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="是否线下打款" align="center" key="payType">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.payType"
                active-value="1"
                inactive-value="0"
                @change="handlePayTypeChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="代付通道" align="center" prop="payChannel">
            <template slot-scope="scope">
              <span v-if="scope.row.payType == 0">
                <span v-for="item in payConfigList" v-if="scope.row.payChannel == item.id">{{item.name}}</span>
              </span>
            </template>
          </el-table-column>

          <el-table-column label="是否线下还款" align="center" key="paymentType">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.paymentType"
                active-value="1"
                inactive-value="0"
                @change="handlePaymentTypeChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>

          <el-table-column label="代收通道" align="center" prop="payChannel">
            <template slot-scope="scope">
              <span v-if="scope.row.paymentType == 0">
                <span v-for="item in payConfigList" v-if="scope.row.payChannel == item.id">{{item.name}}</span>
              </span>
              <span v-else>
                <span v-for="item in payBankList" v-if="scope.row.payBankId == item.id">{{item.name}}(线下银行卡)</span>
              </span>
            </template>
          </el-table-column>

          <el-table-column label="创建时间" align="center" prop="createTime" v-if="columns[2].visible" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="160"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope" v-if="scope.row.userId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:company:remove']"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="是否开启线上打款" prop="payType">
              <el-radio-group v-model="form.payType">
                <el-radio
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.id"
                >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="代付支付通道" prop="payChannel" v-if="this.form.payType == '0'">
              <el-radio-group v-model="form.payChannel">
                <el-radio
                  v-for="item in payConfigList"
                  :key="item.id"
                  :label="item.id"
                >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="24">
            <el-form-item label="是否开启线上还款" prop="paymentType">
              <el-radio-group v-model="form.paymentType">
                <el-radio
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.id"
                >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="线下收款银行卡" prop="payBankId" v-if="this.form.paymentType == '1'">
              <el-radio-group v-model="form.payBankId">
                <el-radio
                  v-for="item in payBankList"
                  :key="item.id"
                  :label="item.id"
                >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="24">
            <el-form-item label="还款支付通道" prop="paymentChannel" v-if="this.form.paymentType == '0'">
              <el-radio-group v-model="form.paymentChannel">
                <el-radio
                  v-for="item in payConfigList"
                  :key="item.id"
                  :label="item.id"
                >{{ item.name }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="logo" prop="logo">
              <imageSingleUpload v-model="form.logo"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {listCompany, getCompany, addCompany, updateCompany, changeStatus, delCompany,changePayTypeStatus,changePaymentTypeStatus,changePublicOpen} from "@/api/company/index";
import {payBankList} from "@/api/app/paybank"
import {list} from '@/api/app/pay/config'
export default {
  name: "Company",
  dicts: ['sys_normal_disable', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      payConfigList: null,
      payBankList: null,
      previewListResource: true,
      // 日期范围
      dateRange: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      // 表单参数
      form: {},
      typeList: [
        {
          id: '0',
          name: '是'
        },
        {
          id: '1',
          name: '否'
        }
      ],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      // 列信息
      columns: [
        {key: 0, label: `公司名称`, visible: true},
        {key: 1, label: `状态`, visible: true},
        {key: 3, label: `创建时间`, visible: true}
      ],
      // 表单校验
      rules: {
        name: [
          {required: true, message: "公司名称不能为空", trigger: "blur"},
          {min: 2, max: 50, message: '用户名称长度必须介于 2 和 50 之间', trigger: 'blur'}
        ],
        logo: [
          {required: true, message: "公司logo不能为空", trigger: "blur"},
        ]
      }
    };
  },
  watch: {},
  created() {
    this.getList();
    this.getPayConfigList();
    this.getPayBankList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listCompany(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    getPayConfigList(){
      list().then(response => {
          this.payConfigList = response.data;
        }
      );
    },

    getPayBankList(){
      payBankList().then(response => {
          this.payBankList = response.data;
        }
      );
    },

    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "0" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text + '""' + row.name + '"吗？').then(function () {
        return changeStatus(row.userId, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.status = row.status === "0" ? "1" : "0";
      });
    },
    //是否开始公债明细平台显示
    handlePublicOpenChange(row) {
      let text = row.publicOpen === "0" ? "开启" : "关闭";
      this.$modal.confirm('确认要"' + text + '"公债明细平台名称吗？').then(function () {
        return changePublicOpen(row.id, row.publicOpen);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.publicOpen = row.publicOpen === "0" ? "1" : "0";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        logo: undefined,
        paymentType: '0',
        payType: '0',
        payBankId: undefined,
        paymentChannel: undefined,
        payChannel: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公司";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCompany(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公司";
      });
    },

    handlePayTypeChange(row) {
      let text = row.payType === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text +  '"线下打款吗？').then(function() {
        return changePayTypeStatus(row.id, row.payType);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.payType = row.payType === "0" ? "1" : "0";
      });
    },

    handlePaymentTypeChange(row) {
      let text = row.paymentType === "1" ? "启用" : "停用";
      this.$modal.confirm('确认要"' + text +  '"线上还款吗？').then(function() {
        return changePaymentTypeStatus(row.id, row.paymentType);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.paymentType = row.paymentType === "0" ? "1" : "0";
      });
    },


    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除公司编号为"' + ids + '"的数据项？').then(function () {
        return delCompany(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
